package com.cn.jinl.controller;

import com.cn.jinl.api.IDeviceService;
import com.cn.jinl.domain.Device;
import com.cn.jinl.domain.DeviceReportVo;
import com.cn.jinl.utils.CommonUtil;
import com.cn.jinl.utils.JsonResponse;
import com.cn.jinl.utils.QueryForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created On : 2023/12/11.
 * <p>
 * Author : Huiyan
 * <p>
 * Description: 基础数据----设备注册
 */
@Controller
@RequestMapping("/device")
public class DeviceController {

    @Autowired
    private IDeviceService deviceService;


    /**
     * @author : Huiyan feng
     * @date   : 2023/12/11
     * @param  : [queryForm, frontToken]
     * @return : com.cn.jinl.utils.JsonResponse<java.util.List<com.cn.jinl.domain.Device>>
     * @description : getDeviceSelect 根据条件查询设备
     */
    @RequestMapping("/getDeviceSelect/{frontToken}")
    @ResponseBody
    public JsonResponse<List<Device>> getDeviceSelect(@RequestBody QueryForm queryForm,@PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<List<Device>> response = new JsonResponse<>();
        List<Device> devices = deviceService.getDeviceSelect(queryForm);
        response.setResult(devices);
        response.setSuccess(true);
        return response;
    }

    //获取罐号设备
    @RequestMapping("/getDeviceGuanHao/{frontToken}")
    @ResponseBody
    public JsonResponse<List<Device>> getDeviceGuanHao(String deviceCode,@PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<List<Device>> response = new JsonResponse<>();
        List<Device> devices = deviceService.getDeviceGuanHao(deviceCode);
        response.setResult(devices);
        response.setSuccess(true);
        return response;
    }

    /**
     * @author : Huiyan feng
     * @date   : 2023/12/11
     * @param  : [device, frontToken]
     * @return : com.cn.jinl.utils.JsonResponse<java.lang.String>
     * @description : updateDevice 修改设备数据
     */
    @RequestMapping("/updateDevice/{frontToken}")
    @ResponseBody
    public JsonResponse<String> updateDevice(@RequestBody Device device,@PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = deviceService.updateDevice(device);
        return CommonUtil.getResponseData(response,result);
    }

    /**
     * @author : Huiyan feng
     * @date   : 2023/12/11
     * @param  : [queryForm, frontToken]
     * @return : com.cn.jinl.utils.JsonResponse<java.lang.String>
     * @description : deleteDevice 删除选中的设备数据
     */
    @RequestMapping("/deleteDevice/{frontToken}")
    @ResponseBody
    public JsonResponse<String> deleteDevice(@RequestBody QueryForm queryForm,@PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = deviceService.deleteDevice(queryForm);
        return CommonUtil.getResponseData(response,result);
    }


    /**
     * @author : Huiyan feng
     * @date   : 2023/12/11
     * @param  : [devices, frontToken]
     * @return : com.cn.jinl.utils.JsonResponse<java.lang.String>
     * @description : addDevice 增加设备数据
     */
    @RequestMapping("/addDevice/{frontToken}")
    @ResponseBody
    public JsonResponse<String> addDevice(@RequestBody List<Device> devices,@PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = deviceService.addDevice(devices);
        return CommonUtil.getResponseData(response,result);
    }

    @RequestMapping("/api/getDeviceInfoByCode")
    @ResponseBody
    public JsonResponse<Device> getDeviceInfoByCode(String deviceCode) throws Exception{
        JsonResponse<Device> response = new JsonResponse<Device>();
        Device device = deviceService.getDeviceInfoByCode(deviceCode);
        if(null == device){
            response.setSuccess(false);
            response.setMessage("未查询到设备");
        }else if(device.getDeviceStatus().equals("1")){
            response.setSuccess(false);
            response.setResult(device);
            response.setMessage("设备已经停用！");
        }else {
            response.setResult(device);
            response.setSuccess(true);
        }
        return response;
    }


    //扫码受理激光头接口
    @RequestMapping("/api/getDeviceReportByCode")
    @ResponseBody
    public JsonResponse<DeviceReportVo> getDeviceReportByCode(String deviceCode) throws Exception{
        JsonResponse<DeviceReportVo> response = new JsonResponse<DeviceReportVo>();
        DeviceReportVo deviceReportVo = deviceService.getDeviceReportByCode(deviceCode);
        if(null == deviceReportVo){
            response.setSuccess(false);
            response.setMessage("该设备为查询到受理任务");
        }else if(deviceReportVo.getDeviceStatus().equals("1")){
            response.setSuccess(false);
            response.setResult(deviceReportVo);
            response.setMessage("设备已经停用！");
        }else {
            response.setResult(deviceReportVo);
            response.setSuccess(true);
        }
        return response;
    }
}
